﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//Salty:由FacilityFactory修改而来，使用创建者模式（谁创建谁管理）
public class BuildingManager : MonoBehaviour
{
    public static BuildingManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<BuildingManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("BuildingManager");
                _Instance = obj.AddComponent<BuildingManager>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static BuildingManager _Instance;

    private List<GameObject> buildingList = new List<GameObject>();

    [System.Serializable]
    public struct BuildingInfo
    {
        public BuildingType type;
        public int AP;
        public ResourceType resource;
        public float cost;
        public GameObject prefab;
        public Sprite ruin;
    }

    [SerializeField]
    public BuildingInfo[] buildingPrefabs;
    Dictionary<BuildingType, BuildingInfo> buildingDict = new Dictionary<BuildingType, BuildingInfo>();
    //public Dictionary<BuildingType, Sprite> spriteDict = new Dictionary<BuildingType, Sprite>();

    void Awake()
    {
        foreach(BuildingInfo info in buildingPrefabs)
        {
            if(!buildingDict.ContainsKey(info.type))
            {
                buildingDict[info.type] = info;
            }
        }
    }

    //更改为实例化, 记录位置, 并返回
    public GameObject CreateBuilding(BuildingType type)
    {
        if (buildingDict.ContainsKey(type))
        {
            //此处还需添加建筑对传入位置的记录
            GameObject obj = Instantiate(buildingDict[type].prefab, Vector3.zero, Quaternion.identity);            
            buildingList.Add(obj);
            return obj;
        }
        else return null;
    }

    //返回行动力
    public int GetRequiredAP(BuildingType type)
    {
        if (buildingDict.ContainsKey(type))
        {
            return buildingDict[type].AP;
        }
        return 0;
    }

    public Sprite GetRuinSprite(BuildingType type)
    {
        if(buildingDict.ContainsKey(type))
        {
            return buildingDict[type].ruin;
        }
        return null;
    }

    public Sprite GetNormalSprite(BuildingType type)
    {
        if (buildingDict.ContainsKey(type))
        {
            return buildingDict[type].prefab.GetComponent<SpriteRenderer>().sprite;
        }
        return null;
    }

    public ResourceType GetRequiredResourceType(BuildingType type)
    {
        if (buildingDict.ContainsKey(type))
        {
            return buildingDict[type].resource;
        }
        return ResourceType.None;
    }

    public float GetRequiredCost(BuildingType type)
    {
        if (buildingDict.ContainsKey(type))
        {
            return buildingDict[type].cost;
        }
        return 0;
    }

    public bool HasBuilding(GameObject obj)
    {
        return buildingList.Contains(obj);
    }
    
    public bool DestroyBuilding(GameObject obj)
    {
        if(buildingList.Remove(obj))
        {
            Destroy(obj);
        }
        return true;
    }
    
}
